<template>
	<view class="container">
		<view class="header">
			<dropDownMenu v-if="show" class="downMenu" :titleArr="titleArr" :dropArr="dropArr" @finishDropClick="finishClick"></dropDownMenu>
			<view class="total">数量：{{ rooms_total }}</view>
		</view>
		<roomList ref="roomList" :query="form" @getTotal="setTotal"></roomList>
		<keyWord :words="form.keyword" @onChange="setKeyWord" />
		<view class="addBtn" @click="addHouse">
			<uni-icons type="plusempty" size="32" color="#fff"></uni-icons>
		</view>
	</view>
</template>

<script>
import api from '@/api/api';

export default {
	data() {
		return {
			form: {
				keyword: '',
				room_type: '1',
				building_id: '0',
				group_id: '',
				community_id: '',
				room_status: '',
				updateNum: 0
			},
			rooms_total: 0,
			current: 0,
			titleArr: ['分组', '小区', '状态', ''],
			dropArr: [[], [], []],
			show: false
		};
	},
	onLoad() {
		this.show = false;
		this.getGroup();
	},
	onShow() {
		this.form.updateNum++;
	},
	onPullDownRefresh() {
		this.form.updateNum++;
	},
	methods: {
		navChange(e) {
			this.current = e.currentIndex;
		},
		finishClick(resultData) {
			console.log(resultData);
			this.form.group_id = resultData[0];
			this.form.community_id = resultData[1];
			this.form.room_status = resultData[2];
		},
		setKeyWord(e) {
			this.form.keyword = e;
		},
		setTotal(num) {
			this.rooms_total = num;
			uni.stopPullDownRefresh();
		},
		addHouse() {
			uni.navigateTo({
				url: '/pages/add/house'
			});
		},
		getGroup() {
			api.groupList({}).then((data) => {
				let list = data.data;
				let arr = [];
				for (let i = 0; i < list.length; i++) {
					let base = {
						text: list[i].group_name,
						value: list[i].id
					};
					arr.push(base);
				}
				let all = {
					text: '全部',
					value: ''
				};
				arr.unshift(all);
				this.dropArr[0] = arr;
				this.getCommunity();
			});
		},
		getCommunity() {
			api.communityList({}).then((data) => {
				let list = data.data;
				let arr = [];
				for (let i = 0; i < list.length; i++) {
					let base = {
						text: list[i].community_name,
						value: list[i].id
					};
					arr.push(base);
				}
				let all = {
					text: '全部',
					value: ''
				};
				arr.unshift(all);
				this.dropArr[1] = arr;
				this.getDict();
			});
		},
		getDict() {
			api.getDict('room_status').then((data) => {
				let list = data.data;
				let arr = [];
				for (let i = 0; i < list.length; i++) {
					let base = {
						text: list[i].item_name,
						value: list[i].item_code
					};
					arr.push(base);
				}
				let all = {
					text: '全部',
					value: ''
				};
				arr.unshift(all);
				this.dropArr[2] = arr;
				this.show = true;
			});
		}
	}
};
</script>
